﻿using ETA.ClientService;
using ETA.Entity;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    public class ExpandController : BaseController
    {
        private static IReferrerLogService _IReferrerLogService { set; get; }
        private static IUserService _IUserService { set; get; }

        public ExpandController(IReferrerLogService IReferrerLogService, IUserService IUserService)
        {
            _IReferrerLogService = IReferrerLogService;
            _IUserService = IUserService;
        }

        //
        // GET: /Expand/

        public ActionResult Index()
        {
            var list = new List<ReferrerLog>();
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogList(
            new QueryReferrerLogRequest
            {
                UserId = CurrentUser.UserId,
                Page = 0,
                PageSize = int.MaxValue
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = response.ReferrerLogs.ToList();
            }

            var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
            if (user != null)
            {
                ViewBag.IsReferrer = user.IsReferrer;
            }
            return View(list);
        }

        public ActionResult GetReferrerLogList(int page, int pageSize = 10, string keyWord = null)
        {
            var list = new List<ReferrerLog>();
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogList(
            new QueryReferrerLogRequest
            {
                UserId = CurrentUser.UserId,
                Page = page,
                PageSize = pageSize
            }, out count
            );
            if (response.Result.IsSuccess)
            {
                list = response.ReferrerLogs.ToList();
            }
            return JsonWithDate(list);
        }



        public ActionResult GetReferrerLogCount()
        {
            var count = 0;
            var response = _IReferrerLogService.QueryReferrerLogList(
           new QueryReferrerLogRequest
           {
               UserId = CurrentUser.UserId
           }, out count
           );
            return JsonWithDate(count);
        }

        /// <summary>
        /// 成为快车之友
        /// </summary>
        /// <returns></returns>
        public ActionResult BeReferrer()
        {
            bool res = _IUserService.UpdateUserIsReferrer(CurrentUser.UserId, 1);
            return JsonWithDate(res);
        }


    }
}
